package ch.il07.converter.persistence;

import java.util.ResourceBundle;

import ch.il07.converter.persistence.exceptions.PersistenceException;

public final class PersistenceHelper {
	
	private static final String IMPL_CLASS_PARAM = "persistence_impl_class";

	private static final ResourceBundle config = ResourceBundle
			.getBundle("ch.il07.converter.persistence.mapping");
	
	private static Persistence instance;

	private PersistenceHelper() {}

	public static Persistence getPersistence() throws PersistenceException {
		if(instance == null) {
			instance = getImplementation();
		}
		return instance;
	}
	
	@SuppressWarnings("unchecked")
	private static Persistence getImplementation() throws PersistenceException {
		String implClassName = config.getString(IMPL_CLASS_PARAM);
		try {
			Class<Persistence> implClass = (Class<Persistence>) Class.forName(implClassName);
			return implClass.newInstance();
		} catch (Exception e) {
			throw new PersistenceException(e);
		}
	}

}
